package com.shopping_back.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shopping_back.pojo.entity.Sku;
import com.shopping_back.mapper.SkuMapper;
import com.shopping_back.service.ISkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 商品SKU表 服务实现类
 * </p>
 *
 * @author Jack
 * @since 2025-07-12
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements ISkuService {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Sku findSkuBySpecs(Long spuId, Map<String, String> specs) {
        try {
            // 将规格Map转换为JSON字符串
            String specsJson = objectMapper.writeValueAsString(specs);

            // 查询匹配的SKU
            return getOne(new LambdaQueryWrapper<Sku>()
                    .eq(Sku::getSpuId, spuId)
                    .eq(Sku::getSpecsJson, specsJson));
        } catch (Exception e) {
            throw new RuntimeException("查询SKU失败", e);
        }
    }
}
